package cn.lisens.influxdb.controller;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.lisens.influxdb.entity.ElecUsage;
import cn.lisens.influxdb.entity.Temperature;
import cn.lisens.influxdb.model.DayUsage;
import cn.lisens.influxdb.service.TemperatureService;

@RestController
@RequestMapping("/api/temperature")
public class TemperatureController {
	
	@Autowired
	private TemperatureService temperatureService;

	@RequestMapping("/qurey")
    public List<DayUsage> getTemperatures(String location) {
        Instant end = Instant.now();
        Instant start = end.minus(1, ChronoUnit.HOURS);
        
        return temperatureService.query(location, start, end);
    }
	
	@RequestMapping("/listData")
    public List<Temperature> listData(String location) {
        Instant end = Instant.now();
        Instant start = end.minus(1, ChronoUnit.HOURS);
        
        return temperatureService.findList(location);
    }
	
	@RequestMapping("/dailyListData")
    public List<ElecUsage> dailyListData(String location) {
        Instant end = Instant.now();
        Instant start = end.minus(1, ChronoUnit.HOURS);
        
        return temperatureService.findDailyList(start);
    }
	
	@RequestMapping("/save")
	public String save(Temperature temperature) {
//		Temperature temperature = new Temperature();
        temperature.setLocation("south");
        temperature.setValue(43D);
        temperature.setTime(Instant.now());
        temperatureService.save(temperature);
        return "保存成功";
	}
}
